home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / mricon / source / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-19  |  5.7 KB  |  285 lines

  1. #define    TRUE    1
  2. #define    FALSE    0
  3. #define    ERR    (-1)
  4.  
  5. #define BUFLEN  128
  6.  
  7.     int         fext_top=0;
  8.     int         fext_pos=0;
  9.     char far    *dta_bak;
  10.     char         path_top[BUFLEN];
  11.     struct {
  12.         unsigned char    dd_dmy;
  13.         unsigned char    dd_drv;
  14.         unsigned char    dd_path[8];
  15.         unsigned char    dd_extn[3];
  16.         unsigned char    dd_resv[8];
  17.         unsigned char    dd_att;
  18.         unsigned short    dd_time,dd_date;
  19.         unsigned long    dd_size;
  20.         char        dd_name[14];
  21.     } dta;
  22.  
  23. static    int    fext_chk=FALSE;
  24. static  char    exec_chk=FALSE;
  25. static  char    path_chk=FALSE;
  26. static  char    *path_pos;
  27. static  char    *wild_pos;
  28. static  char    wild_buf[BUFLEN];
  29. static  char    path_buf[BUFLEN];
  30.  
  31. /******** Compile to HIS.ASM ***********
  32.  
  33. static    char    tolow(char ch)
  34. {
  35.     if ( ch >= 'A' && ch <= 'Z' )
  36.         return (ch | 0x20);
  37.     else
  38.         return ch;
  39. }
  40. static    void    strlow(register char *str)
  41. {
  42.     while ( *str != '\0' ) {
  43.         *str = tolow(*str);
  44.         str++;
  45.     }
  46. }
  47. static    void    DTA_init()
  48. {
  49.     union REGS regs;
  50.     struct SREGS seg;
  51.     char far *p;
  52.  
  53.     regs.h.ah = 0x2F;
  54.     intdosx(®s,®s,&seg);
  55.     FP_SEG(dta_bak) = seg.es;
  56.     FP_OFF(dta_bak) = regs.x.bx;
  57.  
  58.     p = (char far *)&dta;
  59.     regs.h.ah = 0x1a;
  60.     seg.ds = FP_SEG(p);
  61.     regs.x.dx = FP_OFF(p);
  62.     intdosx(®s,®s,&seg);
  63. }
  64. static    void    DTA_end()
  65. {
  66.     union REGS regs;
  67.     struct SREGS seg;
  68.  
  69.     regs.h.ah = 0x1a;
  70.     seg.ds = FP_SEG(dta_bak);
  71.     regs.x.dx = FP_OFF(dta_bak);
  72.     intdosx(®s,®s,&seg);
  73. }
  74. static    int    farst_call(char *wild)
  75. {
  76.     union REGS regs;
  77.     struct SREGS seg;
  78.     char far *p;
  79.  
  80.     p = wild;
  81.     regs.h.ah = 0x4e;
  82.     regs.x.cx = 0x21;
  83.     seg.ds = FP_SEG(p);
  84.     regs.x.dx = FP_OFF(p);
  85.     intdosx(®s,®s,&seg);
  86.     return (regs.x.cflag == 0 ? TRUE:FALSE);
  87. }
  88. static    int    next_call(void)
  89. {
  90.     union REGS regs;
  91.  
  92.     regs.h.ah = 0x4f;
  93.     intdos(®s,®s);
  94.     return (regs.x.cflag == 0 ? TRUE:FALSE);
  95. }
  96. ******** End of Compile ***********/
  97.  
  98. static    int     path_set(void)
  99. {
  100.     char    *p,*s;
  101.  
  102.     if ( *path_pos == '\0' )
  103.         return FALSE;
  104.  
  105.     p = wild_buf;
  106.     while ( *path_pos != '\0' && p < &(wild_buf[BUFLEN-1]) ) {
  107.         if ( *path_pos == ';' ) {
  108.             path_pos++;
  109.             break;
  110.         } else
  111.             *(p++) = *(path_pos++);
  112.     }
  113.  
  114.     if ( p > wild_buf && *(p-1) != '\\' && p < &(wild_buf[BUFLEN-1]) )
  115.         *(p++) = '\\';
  116.  
  117.     s = path_buf;
  118.     while ( *s != '\0' && p < &(wild_buf[BUFLEN-1]) )
  119.         *(p++) = *(s++);
  120.     *p = '\0';
  121.  
  122.     return TRUE;
  123. }
  124. static    int     file_cmp(void)
  125. {
  126.     char *p;
  127.  
  128.     p = dta.dd_name;
  129.     while ( *p != '.' && *p != '\0' )
  130.     p++;
  131.  
  132.     if ( exec_chk ) {
  133.         if ( strcmp(p,".BAT") == 0 ||
  134.              strcmp(p,".COM") == 0 ||
  135.              strcmp(p,".EXE") == 0 ||
  136.              strcmp(p,".EXP") == 0 )
  137.             return TRUE;
  138.         else
  139.             return FALSE;
  140.     } else
  141.         return TRUE;
  142. }
  143. static    int     farst_dir(void)
  144. {
  145.     return farst_call(wild_buf);
  146. }
  147. static    int     next_dir(void)
  148. {
  149.     do {
  150.         while ( !next_call() ) {
  151.             do {
  152.                 if ( !path_chk   || !path_set() )
  153.                     return FALSE;
  154.             } while ( !farst_dir() );
  155.             break;
  156.         }
  157.     } while ( !file_cmp() );
  158.  
  159.     return TRUE;
  160. }
  161. static    int     wild_set(char *str,int len)
  162. {
  163.     int     n;
  164.     char    *p;
  165.  
  166.     n = len;
  167.     while ( n > 0 ) {
  168.         n--;
  169.         if ( str[n] == ' ' || str[n] == '\t' ) {
  170.             n++;
  171.             break;
  172.         }
  173.     }
  174.  
  175.     exec_chk = path_chk = (n > 0 ? FALSE:TRUE);
  176.  
  177.     fext_top = fext_pos = n;
  178.     p = wild_pos = wild_buf;
  179.     while ( n < len && p < &(wild_buf[BUFLEN-1]) ) {
  180.         if ( str[n] == ':' || str[n] == '\\' ) {
  181.             fext_pos = n + 1;
  182.             wild_pos = p + 1;
  183.             path_chk = FALSE;
  184.         }
  185.         *(p++) = str[n++];
  186.     }
  187.     *p = '\0';
  188.  
  189.     n = 0;
  190.     p = wild_pos;
  191.     while ( *p != '\0' ) {
  192.         if ( *p == '.' )
  193.             n |= 2;
  194.         else if ( *p == '*' || *p == '?' )
  195.             n |= 1;
  196.         p++;
  197.     }
  198.  
  199.     switch(n) {
  200.     case 0: strcpy(p,"*.*"); break;
  201.     case 1: strcpy(p,".*"); break;
  202.     case 2: case 3: exec_chk = FALSE; break;
  203.     }
  204.  
  205.     if ( path_chk ) {
  206.         strcpy(path_buf,wild_buf);
  207.     wild_pos = path_buf + ((int)wild_pos - (int)wild_buf);
  208.         path_pos = path_top;
  209.     }
  210.  
  211.     if ( farst_dir() && file_cmp() )
  212.         return TRUE;
  213.     else
  214.         return next_dir();
  215. }
  216.  
  217. char    *file_ext(char *str,int pos)
  218. {
  219.     int    rc;
  220.     char   *p;
  221.  
  222.     if ( fext_chk == FALSE ) {
  223.         DTA_init();
  224.         rc = wild_set(str,pos);
  225.         fext_chk = TRUE;
  226.     } else
  227.         rc = next_dir();
  228.  
  229.     if ( rc == FALSE ) {
  230.         DTA_end();
  231.         fext_chk = FALSE;
  232.     BEEP();
  233.         return wild_pos;
  234.     }
  235.  
  236.     strlow(dta.dd_name);
  237.  
  238.     if ( exec_chk ) {
  239.     p = dta.dd_name;
  240.     while ( *p != '.' && *p != '\0' )
  241.         p++;
  242.     *p = '\0';
  243.     }
  244.  
  245.     return dta.dd_name;
  246. }
  247. void    file_end(void)
  248. {
  249.     if ( fext_chk != FALSE ) {
  250.     while ( next_call() );
  251.         DTA_end();
  252.         fext_chk = FALSE;
  253.     }
  254. }
  255. /******** Compile to HIS.ASM ***********
  256. static    int     envcmp(char far *p,char *s)
  257. {
  258.     while ( *s != '\0' ) {
  259.        if ( tolow(*p) != *s )
  260.             return FALSE;
  261.     p++;
  262.     s++; 
  263.     }
  264.     return TRUE;
  265. }
  266. void    Path_init(void)
  267. {
  268.     short far *s;
  269.  
  270.     FP_SEG(s) = _psp;
  271.     FP_OFF(s) = 0x2C;
  272.  
  273.     FP_SEG(path_top) = *s;
  274.     FP_OFF(path_top) = 0;
  275.  
  276.     while ( *path_top != '\0' ) {
  277.     if ( envcmp(path_top,"path=") ) {
  278.         path_top += 5;
  279.         break;
  280.     }
  281.         while ( *(path_top++) != '\0' );
  282.     }
  283. }
  284. ******** End of Compile ***********/
  285.